package com.nanhai.buddhist.college.module.study.service.studyplan;

import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.nanhai.buddhist.college.module.study.controller.admin.studyplan.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyplan.StudyPlanSubjectDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.study.convert.studyplan.StudyPlanSubjectConvert;
import com.nanhai.buddhist.college.module.study.dal.mysql.studyplan.StudyPlanSubjectMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.study.enums.ErrorCodeConstants.*;

/**
 * 教学计划课程信息 Service 实现类
 *
 * @author xqenzo@163.com
 */
@Service
@Validated
public class StudyPlanSubjectServiceImpl implements StudyPlanSubjectService {

    @Resource
    private StudyPlanSubjectMapper planSubjectMapper;

    @Override
    public Long createPlanSubject(StudyPlanSubjectCreateReqVO createReqVO) {
        // 插入
        StudyPlanSubjectDO planSubject = StudyPlanSubjectConvert.INSTANCE.convert(createReqVO);
        planSubjectMapper.insert(planSubject);
        // 返回
        return planSubject.getId();
    }

    @Override
    public void updatePlanSubject(StudyPlanSubjectUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePlanSubjectExists(updateReqVO.getId());
        // 更新
        StudyPlanSubjectDO updateObj = StudyPlanSubjectConvert.INSTANCE.convert(updateReqVO);
        planSubjectMapper.updateById(updateObj);
    }

    @Override
    public void deletePlanSubject(Long id) {
        // 校验存在
        this.validatePlanSubjectExists(id);
        // 删除
        planSubjectMapper.deleteById(id);
    }

    private void validatePlanSubjectExists(Long id) {
        if (planSubjectMapper.selectById(id) == null) {
            throw exception(PLAN_SUBJECT_NOT_EXISTS);
        }
    }

    @Override
    public StudyPlanSubjectDO getPlanSubject(Long id) {
        return planSubjectMapper.selectById(id);
    }

    @Override
    public List<StudyPlanSubjectDO> getPlanSubjectList(Collection<Long> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : planSubjectMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StudyPlanSubjectDO> getPlanSubjectPage(StudyPlanSubjectPageReqVO pageReqVO) {
        return planSubjectMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StudyPlanSubjectDO> getPlanSubjectList(StudyPlanSubjectExportReqVO exportReqVO) {
        return planSubjectMapper.selectList(exportReqVO);
    }

}
